<template>
  <div class="card-list-container m-5 p-4" style="height: calc(100vh - 80px)">
    <div class="top-line-container">
      <div class="flex gap-6 items-center px-3">
        <div class="flex items-center gap-4">
          <span> 行业领域 </span>
          <el-button
            size="small"
            plain
            type="primary"
            :icon="isEditTags ? 'Edit' : 'Setting'"
            @click="handleSetTag"
            >管理</el-button
          >
        </div>
        <div class="flex items-center gap-2">
          <div
            v-for="(item, index) in induList"
            :key="index"
            class="indu-tag px-4 py-1 text-sm relative cursor-pointer"
            :class="{ active: actvieIndu?.name === item.name }"
            @click="handleTagClick(item)"
          >
            {{ item.name }}
            <div
              v-if="isEditTags && actvieIndu?.name !== item.name"
              class="absolute -top-1 -right-1 cursor-pointer"
              @click.stop="handleTagDelete(item)"
            >
              <el-icon color="red"><CircleCloseFilled /></el-icon>
            </div>
          </div>
          <div class="ml-2 cursor-pointer" v-if="isEditTags" @click="handleTagAdd">
            <el-icon size="25" color="#409EFF"><Plus /></el-icon
          ></div>
        </div>
      </div>
    </div>
    <div class="main-wrapper pt-4 flex items-stretch">
      <div class="knowledge-conainter pr-6 flex-none">
        <div class="flex items-center justify-between">
          <div class="flex gap-1 items-center text-sm">
            <img src="@/assets/images/ai/zs-icon.png" alt="" />
            <div> 知识库清单</div>
          </div>
          <el-button
            v-if="cateList.length"
            plain
            size="small"
            type="primary"
            icon="Plus"
            @click="handleCreateKbase"
          >
            新建知识库
          </el-button>
        </div>

        <div class="pt-3" v-if="actvieIndu">
          <!-- 分类 -->
          <div v-for="(cItem, cIndex) in cateList" :key="cIndex">
            <div class="flex justify-between items-center py-1 opacity-45 text-sm">
              <div> {{ cItem.name }}</div>
              <el-dropdown
                @command="
                  (val) => {
                    handleCateCommand(val, cItem);
                  }
                "
              >
                <el-icon><MoreFilled /></el-icon>
                <template #dropdown>
                  <el-dropdown-item command="Edit">修改</el-dropdown-item>
                  <el-dropdown-item command="Add">新增</el-dropdown-item>
                  <el-dropdown-item command="Del">删除</el-dropdown-item>
                </template>
              </el-dropdown>
              <!-- <div class="cursor-pointer" @click="handleCateMore(cItem)">
                
              </div> -->
            </div>
            <!-- 分类下 知识库 -->
            <div class="overflow-auto pr-2" style="max-height: 160px">
              <div
                v-for="(ckItem, ckIndex) in cItem.sdkLmKbs"
                :key="ckIndex"
                class="catekb-item text-sm flex items-center my-1.5 cursor-pointer"
                :class="{ active: actvieCateKb?.name === ckItem.name }"
                @click="handleCateKbClick(ckItem)"
              >
                <div class="w-3 flex-1 single-line-ellipsis" :title="ckItem.name">{{
                  ckItem.name
                }}</div>
                <div class="flex-none">
                  <el-button
                    link
                    icon="Edit"
                    style="color: #409eff"
                    @click="handlekbEdit(ckItem)"
                  />
                  <el-button
                    link
                    icon="Delete"
                    style="color: red"
                    @click="handlekbDelete(ckItem)"
                  />
                </div>
              </div>
            </div>
          </div>
          <div class="mt-3">
            <el-button link type="primary" icon="Plus" @click="handleCateCommand('Add')">
              新增分类
            </el-button>
          </div>
        </div>
      </div>
      <div class="main-detail-container px-6 flex-1">
        <div class="text-black text-sm kb-detail-container pb-2.5">
          <div class="font-bold">{{ actvieCateKb?.name }}</div>
          <div class="my-3">{{ actvieCateKb?.remark }}</div>
          <div v-if="actvieCateKb?.sceneNname"
            ><span class="font-bold">关联场景</span>：{{ actvieCateKb?.sceneNname }}</div
          >
        </div>

        <div class="py-5">
          <el-tabs>
            <el-tab-pane label="文件列表">
              <sdkLmKbFile v-model:cateKb="actvieCateKb" />
            </el-tab-pane>
            <el-tab-pane label="智库问答">
              <SdkLmKbQa v-model:cateKb="actvieCateKb" />
            </el-tab-pane>
          </el-tabs>
          <!-- <div class="text-black font-bold">文件列表</div> -->
        </div>
      </div>
    </div>

    <lmKbDiaolog
      v-model="kbDialog.open"
      :industryInfo="actvieIndu"
      :cateList="cateList"
      :catekbInfo="kbDialog.info"
      :type="kbDialog.type"
      @refresh="refreshCateList"
    />

    <lmInduDialog
      v-model="induDialog.open"
      :data="induDialog.data"
      :title="induDialog.title"
      @refresh="getIndustryData"
    />

    <lmCateDialog
      v-model="cateDlg.open"
      :industryInfo="actvieIndu"
      :data="cateDlg.data"
      :title="cateDlg.title"
      @refresh="refreshCateList"
    />
  </div>
</template>
<script setup>
  import { ref } from 'vue';
  import { listSdkLmKbIndustry, delSdkLmKbIndustry } from '@/api/intelligencebase/sdkLmKbIndustry';
  import { listSdkLmKbCate, delSdkLmKbCate } from '@/api/intelligencebase/sdkLmKbCate';
  import { delSdkLmKb, getSdkLmKbInfo } from '@/api/intelligencebase/sdkLmKb';
  // import { pageSdkLmKb } from '@/api/intelligencebase/sdkLmKb';
  import SdkLmKbFile from '../sdkLmKbFile.vue';

  import lmKbDiaolog from './lmKbDiaolog';
  import lmInduDialog from './lmInduDialog';
  import lmCateDialog from './lmCateDialog';

  import modal from '@/plugins/modal';
  import SdkLmKbQa from '../sdkLmKbQa.vue';

  const induList = ref([]);
  const actvieIndu = ref(null);
  const induDialog = ref({
    open: false,
    title: '',
    data: null,
  });

  const cateList = ref([]);
  const actvieCate = ref(null);

  const isEditTags = ref(false);
  const actvieCateKb = ref(null);
  const kbDialog = ref({
    open: false,
    type: 'add',
    info: null,
  });

  const cateDlg = ref({
    open: false,
    title: '',
    data: null,
  });

  // 行业管理 按钮点击事件
  function handleSetTag() {
    isEditTags.value = !isEditTags.value;
  }
  // 行业 新增
  function handleTagAdd() {
    induDialog.value = {
      open: true,
      title: '新增行业领域',
      data: null,
    };
  }
  // 行业 删除
  function handleTagDelete(row) {
    const _ids = row?.id;
    modal
      .confirm('是否确认删除行业领域编号为"' + _ids + '"的数据项？')
      .then(() => {
        return delSdkLmKbIndustry(_ids);
      })
      .then(() => {
        getIndustryData();
        modal.msgSuccess('删除成功');
      })
      .catch(() => {});
  }
  // 行业 点击 事件
  function handleTagClick(item) {
    if (isEditTags.value) {
      // 编辑状态
      induDialog.value = {
        open: true,
        title: '编辑行业领域',
        data: item,
      };
    } else {
      // 切换行业并选择
      actvieIndu.value = item;
      getCateList();
    }
  }

  // 行业列表
  async function getIndustryData() {
    const res = await listSdkLmKbIndustry();
    console.log({ indu: res });
    // if (!res.data?.length) {
    //   induList.value = [
    //     { name: '政务服务' },
    //     { name: '基层治理' },
    //     { name: '干部队伍管理' },
    //     { name: '市场监管' },
    //   ];
    //   actvieIndu.value = induList.value[0];
    // } else {
    induList.value = res.data;
    actvieIndu.value = induList.value[0];
    // }

    getCateList();
  }

  // 知识分类
  async function getCateList() {
    const res = await listSdkLmKbCate({}, { industryId: actvieIndu.value.id });
    console.log({ cate: res });
    // if (!res.data?.length) {
    //   cateList.value = [
    //     { name: '未分类' },
    //     { name: '政策文件' },
    //     { name: '流程说明' },
    //     { name: '权责清单' },
    //   ];
    //   actvieCate.value = cateList.value[0];
    // } else {
    cateList.value = res.data;
    actvieCate.value = cateList.value[0];
    actvieCateKb.value = cateList.value.reduce((pre, cur) => {
      pre.push(...cur.sdkLmKbs);
      return pre;
    }, [])[0];
    // }

    // getCateKbList();
  }

  // 刷新分类列表
  function refreshCateList() {
    getCateList();
  }
  function handlekbEdit(row) {
    getSdkLmKbInfo(row.id).then((res) => {
      kbDialog.value = {
        open: true,
        type: 'edit',
        info: res.data,
      };
    });
  }
  // 知识库 删除事件
  function handlekbDelete(row) {
    const _ids = row?.id;
    modal
      .confirm('是否确认删除知识库分类编号为"' + _ids + '"的数据项？')
      .then(() => {
        return delSdkLmKb(_ids);
      })
      .then(() => {
        refreshCateList();
        modal.msgSuccess('删除成功');
      })
      .catch(() => {});
  }

  // // 知识分类中的库列表
  // async function getCateKbList() {
  //   const res = await pageSdkLmKb();
  //   console.log({ cate: res });
  //   if (!res.data?.length) {
  //     cateKbList.value = [
  //       {
  //         name: '政务服务基础事件库',
  //         sceneNname: 'AI办事管家',
  //         describe:
  //           '政务服务基础事件库，包含了人才引进、生育登记办理、开办餐饮店流程、教育资格认证和申请等事件的制度和流程说明文件',
  //       },
  //       { name: '政务服务免审即享' },
  //       { name: '政务服务事件处置全权处置' },
  //       { name: '权责清单' },
  //     ];
  //     actvieCateKb.value = cateKbList.value[0];
  //   } else {
  //     cateKbList.value = res.data;
  //   }
  // }

  // 新增知识分类 按钮 点击事件
  function handleCreateKbase() {
    kbDialog.value = {
      open: true,
      type: 'add',
      info: null,
    };
  }

  // 知识分类项  点击事件
  function handleCateCommand(command, item) {
    console.log({ command });
    switch (command) {
      case 'Edit':
        cateDlg.value = {
          open: true,
          title: '修改知识库分类',
          data: item,
        };
        break;
      case 'Add':
        cateDlg.value = {
          open: true,
          title: '新增知识库分类',
          data: null,
        };
        break;
      case 'Del':
        const _ids = item?.id;
        modal
          .confirm('是否确认删除知识库分类编号为"' + _ids + '"的数据项？')
          .then(() => {
            return delSdkLmKbCate(_ids);
          })
          .then(() => {
            refreshCateList();
            modal.msgSuccess('删除成功');
          })
          .catch(() => {});
        break;
      default:
        break;
    }
  }

  // 知识库 点击事件
  function handleCateKbClick(item) {
    actvieCateKb.value = item;
  }

  getIndustryData();
</script>
<style lang="scss" scoped>
  .top-line-container {
    padding-bottom: 16px;
    border-bottom: 1px solid #ebebeb;
  }

  .card-list-container {
    background: #fff;
    box-shadow: 3px 2px 5px 0 #e0e0e0;
  }

  .indu-tag {
    border-radius: 4px;
    background-color: #f5f5f5;
    color: #888;

    &.active {
      background-color: #409eff;
      color: #fff;
    }
  }

  .main-wrapper {
    height: calc(100vh - 150px);
  }

  .knowledge-conainter {
    width: 320px;
    border-right: 1px solid #ebebeb;
  }

  .catekb-item {
    padding: 4px 8px;
    border: 1px solid #999;
    color: #999;

    &.active {
      border: 1px solid #71b0ff;
      box-shadow: 2px 0 4px 0 #58acff;
      color: #71b0ff;
    }
  }

  .single-line-ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .kb-detail-container {
    border-bottom: 1px solid #ebebeb;
  }
</style>
